<template>
	<view class="container">
		<view style="height: 200rpx;"></view>
		<image :src="$getImgUrl('home/logo.png')" mode="widthFix" class="login-btn"></image>
		<view class="name">潮玩魔盒</view>
		<button type="default" class="default-btn" @click="onLogin">微信登录</button>
		<view class="cancel-view"><text @click="$navTo('pages/shop/shop')">暂不登录</text></view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			couponId: '',
			shareUid: '',
			uuid: ''
		};
	},
	onLoad(options) {
		this.couponId = options.couponId ? options.couponId : '';
		this.shareUid = options.shareUid ? options.shareUid : '';
		this.uuid = options.uuid ? options.uuid : '';
		if (this.couponId != '') {
			this.$store.set('couponId', this.couponId);
		}
		if (this.shareUid != '') {
			this.$store.set('shareUid', this.shareUid);
		}
		if (this.uuid != '') {
			this.$store.set('uuid', this.uuid);
		}
	},
	methods: {
		async onLogin() {
			var self = this;

			// #ifdef H5
			var res = await this.$get('server/login/debuglogin', {
				unionid: 'a1'
			});
			if (res.errcode != 0) {
				this.$toast(res.errmsg);
				return;
			}
			this.$store.set('jwt', res.data.jwt);
			this.$store.set('uid', res.data.user.id);
			this.$navTo('pages/shop/shop');
			// #endif

			// #ifdef MP-WEIXIN
			uni.getUserProfile({
				desc: '登录',
				success: function(res) {
					console.log('用户昵称为：' + JSON.stringify(res));

					uni.login({
						provider: 'weixin',
						success: function(loginRes) {
							// 获取用户信息
							console.log('----------------');
							console.log(loginRes);
							self.xcxLoginApi(loginRes.code, res);
						}
					});
				},
				fail: function(error) {
					console.log('获取失败：' + JSON.stringify(error));
				}
			});
			// #endif
		},
		// 微信登录
		async xcxLoginApi(code, detail) {
			var res = await this.$post('server/login/login', {
				code: code,
				userinfo: detail.rawData,
				quantype: this.$store.get('couponId') ? this.$store.get('couponId') : '',
				shareuid: this.$store.get('shareUid') ? this.$store.get('shareUid') : '',
				uuid: this.$store.get('uuid') ? this.$store.get('uuid') : ''
			});
			if (res.errcode != 0) {
				this.$toast(res.errmsg);
				return;
			}
			console.log('登录成功！！！！！');
			this.$store.set('jwt', res.data.jwt);
			this.$store.set('uid', res.data.user.id);

			var goodsId = this.$store.get('goodsId');
			var page = this.$store.get('page');

			console.log('登录goodsId===', goodsId);
			console.log('登录page===', page);

			if (!this.$isEmpty(goodsId)) {
				this.$redirectTo(`pages/${page}`, {
					goodsId: goodsId
				});
				return;
			}

			this.$navTo('pages/shop/shop');
		}
	}
};
</script>

<style lang="scss">
.login-bg {
	width: 100vw;
	height: 100vh;
	position: absolute;
}
.name {
	text-align: center;
	margin-top: 20rpx;
}
.default-btn {
	width: 560rpx !important;
	position: fixed;
	bottom: 180rpx;
	left: 0;
	right: 0;
}
.cancel-view {
	position: fixed;
	bottom: 100rpx;
	left: 0;
	right: 0;
	text-align: center;

	text {
		color: #aaa !important;
		text-decoration: underline;
	}
}
.login-btn {
	display: block;
	width: 200rpx;
	height: 200rpx;
	margin: 0 auto;
}
</style>
